﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Text;

namespace TraditionalMalteseCuisine.App_Code.Objects
{
    [Serializable]
    public class RecipeLike : BaseTable
    {
        //*************************************************
        //                Class Members
        //*************************************************
        #region Class Members
        public const string TABLE_NAME = "RecipesLikes";
        public const string VIEW_NAME_RECIPES_LIKES_TOTAL_LIKES = "vw" + TABLE_NAME + "TotalLikes";

        public const string RECIPE_ID_TAG = "recipe_" + Recipe.ID_TAG;
        public const string MEMBER_ID_TAG = "member_" + SiteMember.ID_TAG;
        public const string POSTED_DATE_TAG = "posted_date";

        public const string TOTAL_LIKES_TAG = "TotalLikes";

        private int mRecipeID = -1;
        private int mMemberID = -1;
        private DateTime mPostedDate = DateTime.MinValue;
        #endregion
        //*************************************************


        //*************************************************
        //                 Constructor
        //*************************************************
        #region Constructor
        public RecipeLike() : this(-1, -1) { }

        public RecipeLike(int recipe_id, int member_id)
        {
            RecipeID = recipe_id;
            MemberID = member_id;
        }
        #endregion
        //*************************************************


        //*************************************************
        //                   Methods
        //*************************************************
        #region Methods
        protected override string GetInsertScriptUsingParameters()
        {
            StringBuilder insertScript = new StringBuilder();

            insertScript.Append("INSERT INTO ");
            insertScript.AppendFormat("{0} ", TABLE_NAME);
            insertScript.AppendFormat("([{0}], [{1}]) ", RECIPE_ID_TAG, MEMBER_ID_TAG);
            insertScript.Append("VALUES ");
            insertScript.AppendFormat("(@{0}, @{1}) ", RECIPE_ID_TAG, MEMBER_ID_TAG);

            return insertScript.ToString();
        }

        public override OleDbCommand GetInsertCommand()
        {
            OleDbCommand insertCommand = new OleDbCommand();

            insertCommand.CommandType = CommandType.Text;
            insertCommand.CommandText = GetInsertScriptUsingParameters();

            insertCommand.Parameters.AddWithValue(string.Format("@{0}", RECIPE_ID_TAG), ((RecipeID > -1) ? (object)RecipeID : DBNull.Value));
            insertCommand.Parameters.AddWithValue(string.Format("@{0}", MEMBER_ID_TAG), ((MemberID > -1) ? (object)MemberID : DBNull.Value));

            return insertCommand;
        }

        public override BaseTable ReadDataRow(DataRow datarow)
        {
            //ID = int.Parse(datarow[ID_TAG].ToString());
            RecipeID = int.Parse(datarow[RECIPE_ID_TAG].ToString());
            MemberID = int.Parse(datarow[MEMBER_ID_TAG].ToString());
            PostedDate = DateTime.Parse(datarow[POSTED_DATE_TAG].ToString());
            //InUse = bool.Parse(datarow[IN_USE_TAG].ToString());

            return this;
        }


        public override string ToString()
        {
            return string.Format("{0} - {1}", RecipeID, MemberID);
        }
        #endregion
        //*************************************************


        //*************************************************
        //                  Properties
        //*************************************************
        #region Properties
        public int RecipeID
        {
            get { return mRecipeID; }
            set { mRecipeID = value; }
        }

        public int MemberID
        {
            get { return mMemberID; }
            set { mMemberID = value; }
        }

        public DateTime PostedDate
        {
            get { return mPostedDate; }
            set { mPostedDate = value; }
        }
        #endregion
        //*************************************************
    }
}